package top.lyc829.zzl_android.ui.profile.follow.team;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;

import java.util.List;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.FollowTeamFragmentBinding;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.TeamSimpleBean;
import top.lyc829.zzl_android.logic.util.PageInfo;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.base_detail.BaseDetailActivity;
import top.lyc829.zzl_android.ui.team.team_detail.TeamDetailActivity;

public class FollowTeamFragment extends Fragment {

    private FollowTeamViewModel mViewModel;
    FollowTeamAdapter adapter;
    FollowTeamFragmentBinding binding;
    private int otherUserId = -1;

    public static FollowTeamFragment newInstance() {
        return new FollowTeamFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FollowTeamFragmentBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(FollowTeamViewModel.class);

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
        binding.followTeamRecyclerview.setLayoutManager(layoutManager);

        adapter = new FollowTeamAdapter();
        adapter.setAnimationEnable(true);   // recyclerview加载动画
        binding.followTeamRecyclerview.setAdapter(adapter);
       // adapter.setList(new TeamSimpleBean().getExamples());    //logic->team

        //点击跳转至团队介绍
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent=new Intent(getActivity(), TeamDetailActivity.class);
                intent.putExtra("team_id", ((TeamSimpleBean)adapter.getData().get(position)).getId());
                startActivity(intent);
            }
        });


       /* mViewModel.getFollowTeanList();
        mViewModel.getFollowTeamLiveData().observe(getViewLifecycleOwner(), new Observer<List<TeamSimpleBean>>() {
            @Override
            public void onChanged(List<TeamSimpleBean> teamSimpleBeans) {
                adapter.setList(teamSimpleBeans);
            }
        });*/

        request();
        binding.swipeLayout.setRefreshing(true);
        refresh();
        initRefreshLayout();
        //initLoadMore();
        observe();
    }

    private void clickListener() {

        adapter.setOnItemClickListener((adapter1, view, position) -> {
            int temId = adapter.getData().get(position).getId();
            Intent intent = new Intent(getActivity(), TeamDetailActivity.class);
            intent.putExtra("team_id", temId);
            startActivity(intent);
        });


    }
//    private void initLoadMore() {
//        adapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore() {
//                request();
//            }
//        });
//        adapter.getLoadMoreModule().setAutoLoadMore(true);
//        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
//        adapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
//    }

    private void initRefreshLayout() {
        binding.swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        binding.swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    /**
     * 刷新
     */
    private void refresh() {
        // 这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.getLoadMoreModule().setEnableLoadMore(false);
        // 下拉刷新，需要重置页数
        request();
    }
    private void request() {
        if(otherUserId==-1) {
            mViewModel.getFollowTeanList(Utils.getUserId());
        }
        else
        {
            mViewModel.getFollowTeanList(otherUserId);
        }
    }

    //观察者
    private void observe()
    {
        mViewModel.getFollowTeamLiveData().observe(getViewLifecycleOwner(), teamSimpleBeans->
        {
            binding.swipeLayout.setRefreshing(false);
            // 加载完成，等待下一次加载
            adapter.setList(teamSimpleBeans);
            adapter.getLoadMoreModule().loadMoreComplete();
        });

    }
    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }
    public void setOtheruserId(int otherUserId) {
        this.otherUserId = otherUserId;
    }
}